package com.dr.cmdb.core.filedcheck;

import com.dr.cmdb.core.dto.FiledCheckDto;
import com.dr.cmdb.core.dto.ModelPropertyDto;
import com.dr.cmdb.core.enums.FiledCheckEnum;
import com.dr.cmdb.core.errorcode.BizCode;
import com.dr.cmdb.core.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ProjectName: cmdb
 * @Package: com.dr.cmdb.application.filedcheck
 * @Description: 字段校验责任链
 * @Author: duanrui
 * @CreateDate: 2021/3/29 19:25
 * @Version: 1.0
 * <p>
 * Copyright: Copyright (c) 2021
 */
@Order(5)
@Component
@Slf4j
public class FiledCheckSearchHandler extends AbstractFiledCheckHandler {

    @Override
    public FiledCheckEnum getType() {
        return FiledCheckEnum.CHECK_FILED_SEARCH;
    }

    /**
     * 检测字段是否符合规范
     *
     * @param filedCheckDto
     */
    @Override
    public void checkHandler(FiledCheckDto filedCheckDto) {
        Map<String, Object> argument = filedCheckDto.getArgument();
        List<ModelPropertyDto> tableCols = filedCheckDto.getTableCols();
        List<String> collect = tableCols.stream().filter(ModelPropertyDto::getSearchFlag).map(ModelPropertyDto::getPropertyCode).collect(Collectors.toList());
        argument.forEach(
                (k, v) -> {
                    //匹配参数字段和表字段
                    if (!collect.contains(k)) {
                        throw new BizException(BizCode.FIELD_NOT_EXIST.getCode(), "字段非搜索:" + k);
                    }
                }
        );
    }


}
